package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.quest.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void deleteByPrimaryKey(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void insert(Permission record) {
        permissionMapper.insert(record);
    }

    @Override
    public Permission get(Long id) {
        Permission permission = permissionMapper.selectByPrimaryKey(id);
        return permission;
    }

    @Override
    public List<Permission> selectAll() {
        List<Permission> permissions = permissionMapper.selectAll();
        return permissions;
    }

    @Override
    public void updateByPrimaryKey(Permission record) {
        permissionMapper.updateByPrimaryKey(record);
    }

    @Autowired
    private ApplicationContext tx;
    @Override
    public void reload() {
        //获取数据库中,所有的表达式字符串集合
        List<String> expressions = permissionMapper.selectExpressions();

        //方法中,拿到容器对象,获取所有的controller
        Map<String, Object> beans = tx.getBeansWithAnnotation(Controller.class);
        //拿取bean对象集合
        Collection<Object> values = beans.values();
        //遍历
        for (Object value : values) {
            //拿取字节码
            Class aClass = value.getClass().getSuperclass();
            //拿取方法们
            Method[] declaredMethods = aClass.getDeclaredMethods();
            //遍历
            for (Method declaredMethod : declaredMethods) {
                //判断上面是否有注解对象
                RequiresPermissions annotation = declaredMethod.getAnnotation(RequiresPermissions.class);
                if (annotation!=null){
                    //拿取对象属性,封装为权限对象
                    String name = annotation.value()[1];
                    String expression = annotation.value()[0];
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    //如果此表达式与数据库中都不相同
                    if (!expressions.contains(expression)){
                        //保存
                        permissionMapper.insert(permission);
                    }
                }
            }
        }

    }

    @Override
    public PageInfo query(QueryObject qo) {
        //查询符合条件的所有条数
      /*  int i = permissionMapper.selectForCount(qo);
        //如果条数为0,则不用查询具体条数
        if (i==0){
            PageResult<Permission> result = new PageResult<>(1, qo.getPageSize(), 0, Collections.emptyList());
            return result;
        }*/
        //条数不为0查询所有数据
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Permission> permissions = permissionMapper.selectForList(qo);
      /*  if (permissions.size()<=0&&qo.getCurrentPage()>1){
            qo.setCurrentPage(qo.getCurrentPage()-1);
            permissions = permissionMapper.selectForList(qo);
        }

        for (Permission permission : permissions) {
            System.out.println(permission);
        }
        //装进result中
        PageResult<Permission> result = new PageResult<>(qo.getCurrentPage(), qo.getPageSize(), i, permissions);*/
        return new PageInfo(permissions);
    }

    @Override
    public List<String> getExpressionByEmpId(Long id) {
        List<String> expressionByEmpId = permissionMapper.getExpressionByEmpId(id);
        return expressionByEmpId;
    }

}
